<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Auth extends CI_Controller {

    public function Auth() {
        parent::__construct();
        $this->load->model('login_model');
		$this->load->model('log_model');
        
    }

    public function index() {
		if($this->session->userdata('islogin')) redirect('dashboard');
        $data['title'] = 'Login';
		$this->load->view('login_view', $data);
    }

    public function masuk() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $hasil = $this->login_model->validLogin($username, $password);
		if ($hasil['status']) {
            $userdata['id'] = $hasil['id'];
            $userdata['username'] = $hasil['username'];
			$userdata['nama_lengkap'] = $hasil['nama_lengkap'];
            $userdata['role'] = $hasil['role'];
			$userdata['islogin'] = true;
            $this->session->set_userdata($userdata);
			
			$logdata['id_user'] = $userdata['id'];
			$logdata['id_entitas'] = $userdata['id'];
			$logdata['nama_tabel'] = 'loginuser';
			$logdata['action'] = 'login';
			$this->log_model->setLogData($logdata);
			$this->session->set_flashdata('message', 'Anda berhasil login!');
            redirect('dashboard');
        } else {
            redirect('auth');
        }
    }
	
	public function keluar(){
		$logdata['id_user'] = $this->session->userdata('id');
		$logdata['id_entitas'] = $this->session->userdata('id');
		$logdata['nama_tabel'] = 'loginuser';
		$logdata['action'] = 'logout';
		$this->log_model->setLogData($logdata);

		$this->session->sess_destroy();
		redirect('auth');
	}
}
